using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Dream.FirstLevel.Interface;
using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;

namespace Dream.FirstLevel.Base
{
    //mao 24-11-23 07:11
    public abstract class ProductBase : IDeal
    {
        protected Sprite _sprite = null;

        /// <summary>
        ///     Deal该铲平需要等待的时间，单位按照毫秒计算
        /// </summary>
        public int _waitTime = 10000;

        [SerializeField] protected int failPoints;

        /// <summary>
        ///     检查任务是否完成
        /// </summary>
        public bool isComplated;

        public bool isFinished = false;

        /// <summary>
        ///     产品的父容器
        ///     <para></para>
        /// </summary>
        public Player.Player parentContainer;

        [SerializeField] protected int scuessPoints;

        public virtual async UniTaskVoid Deal()
        {
            await UniTask.Delay(_waitTime);
            isComplated = true;
            //mao 24-11-25 08:32
            //对所有product添加在粗发deal函数后可以加入到productList中去
            parentContainer.productList.AddProduct(this);
            parentContainer._waitingProducts.Remove(this);
        }

        /// <summary>
        ///     为获取Sprite进行一个等待
        /// </summary>
        /// <returns></returns>
        public async Task<Sprite> sprite()
        {
            await UniTask.WaitUntil(() => _sprite != null);
            return _sprite;
        }

        public virtual void Success()
        {
            SendEvent(true, scuessPoints);
        }

        public virtual void Fail()
        {
            SendEvent(false, failPoints);
        }


        //mao 24-11-23 07:22
        /// <summary>
        ///     完成事件的发送
        /// </summary>
        /// <param name="result"></param>
        /// <param name="points"></param>
        protected virtual void SendEvent(bool result, int points)
        {
            using var evt = ProductResultEvent.Get();
            evt.isComplated = result;
            evt.pointsChange = points;
            EventManager.SendEvent(evt);
        }

        /// <summary>
        ///     返秒制的等待时间
        /// </summary>
        /// <returns></returns>
        public int GetWaitTimeS()
        {
            return _waitTime / 1000;
        }
    }
}